#include <stdio.h>
#include <assert.h>
#include "../../src/thread.h"

static void * threadfunc(void * arg)
{
  char *name = arg;
  printf("je suis le thread %d, lancé avec l'argument %s\n",
	 thread_self(),name);
  thread_yield();
  printf("je suis encore le thread %d, lancé avec l'argument %s\n",
	 thread_self(), name);
  thread_exit(arg);
}

int main(int argc, char *argv[])
{
  thread_t thread1, thread2;
  void *retval1, *retval2;
  int err;

  setPriority(7);

  printf("\nle main lance 2 threads...\n\n");

  err = thread_create(&thread1, threadfunc, "thread1");
  assert(!err);

  err = thread_create(&thread2, threadfunc, "thread2");
  assert(!err);

  printf("\nle main a lancé les threads %d et %d\n",
	 thread1, thread2);

  printf("le main attend les threads\n\n");
  
  err = thread_join(thread2, &retval2);
  assert(!err);

  err = thread_join(thread1, &retval1);
  assert(!err);

  printf("\nles threads ont terminé en renvoyant '%s' and '%s'\n\n",
	 (char *) retval1, (char *) retval2);

  killMainThread();
  return 0;
}
